package com.mj.app.health.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.mj.app.health.R;
import com.mj.app.health.base.BaseActivity;
import com.mj.app.health.customer.MainTabBtn;
import com.mj.app.health.fragment.AdditionFragment;
import com.mj.app.health.fragment.SelfFragment;
import com.mj.app.health.fragment.ServiceFragment;
import com.mj.app.health.util.ToastUtil;

import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.main_tab_fujia)
    MainTabBtn mainTabFujia;
    @BindView(R.id.main_tab_fuwu)
    MainTabBtn mainTabFuwu;
    @BindView(R.id.main_tab_self)
    MainTabBtn mainTabSelf;

    private MainTabBtn currentTab;
    private static final int QUIT_TIME_LENGTH = 2000;
    private long backTime = 0;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init() {

        initTabs();
        setListener();
    }

    private void setListener() {
        mainTabFujia.setOnClickListener(this);
        mainTabFuwu.setOnClickListener(this);
        mainTabSelf.setOnClickListener(this);
    }

    private void initTabs() {

        mainTabFujia.setmClass(AdditionFragment.class);
        mainTabFuwu.setmClass(ServiceFragment.class);
        mainTabSelf.setmClass(SelfFragment.class);

        clearTabs();
        changTabs(currentTab, mainTabFujia);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_tab_fujia: // 附加
                changTabs(currentTab, mainTabFujia);
                break;
            case R.id.main_tab_fuwu: // 服务
                changTabs(currentTab, mainTabFuwu);
                break;
            case R.id.main_tab_self: // 个人中心
                changTabs(currentTab, mainTabSelf);
                break;
        }
    }

    private void changTabs(MainTabBtn oldTab, MainTabBtn newTab){

        if(oldTab == newTab)
            return;

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        if(oldTab != null){
            oldTab.setSelected(false);
            ft.detach(oldTab.getmFragment());
        }

        if(newTab != null){

            if (newTab.getmFragment() == null) {

                Fragment fragment = Fragment.instantiate(this, newTab.getmClass().getName(), null);
                newTab.setmFragment(fragment);
                ft.add(R.id.m_content, fragment, newTab.getmClass().getName());
            }else
                ft.attach(newTab.getmFragment());

            newTab.setSelected(true);
            this.currentTab = newTab;
        }

        ft.commit();
    }

    private void clearTabs(){

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        List<Fragment> fragments = getSupportFragmentManager().getFragments();

        if (fragments != null && fragments.size() > 0) {

            for (Fragment f : fragments) {
                ft.remove(f);
            }

            ft.commitNow();

        }
    }

    @Override
    public void onBackPressed() {
        long current = System.currentTimeMillis();
        if (current - backTime > QUIT_TIME_LENGTH) {
            backTime = current;
            ToastUtil.show("再次点击返回退出程序");
        } else {
            finish();
        }
    }

}
